﻿using System.Collections.Generic;
using System.Linq;
using AccelerateSensor.Database.SQLite;

namespace AccelerateSensor.Service.DbProxy.SQLite.Dal
{
    internal class NodePrivateAttribute
    {
        private static readonly string TableName = SqliteHelper.TableNodePrivateAttribute;

        /// <summary>
        /// 添加节点私有属性
        /// </summary>
        /// <param name="model">节点私有属性对象</param>
        /// <returns>Rid</returns>
        public int Insert(Models.NodePrivateAttribute model)
        {
            if (model == null)
            {
                return 0;
            }

            var sql = $"insert into {TableName}(NodeUuid, AcquireDataType, " +
                      "     DataLength, SensorFreq, SensorRange, " +
                      "     XAcquireEnable, YAcquireEnable, ZAcquireEnable, " +
                      "     XTrigEnable, YTrigEnable, ZTrigEnable, " +
                      "     TrigValue, HighPassCutoffFreq) " +
                      "values(@NodeUuid, @AcquireDataType, "+
                      "     @DataLength, @SensorFreq, @SensorRange, "+
                      "     @XAcquireEnable, @YAcquireEnable, @ZAcquireEnable, " +
                      "     @XTrigEnable, @YTrigEnable, @ZTrigEnable, " +
                      "     @TrigValue, @HighPassCutoffFreq);";
            return SqliteDbHelper.Insert(sql, model);
        }

        /// <summary>
        /// 批量添加节点私有属性
        /// </summary>
        /// <param name="models">节点私有属性对象集合</param>
        /// <returns>true成功，false失败</returns>
        public bool Insert(List<Models.NodePrivateAttribute> models)
        {
            if (models == null || models.Count == 0)
            {
                return false;
            }

            var sql = $"insert into {TableName}(NodeUuid, AcquireDataType, " +
                      "     DataLength, SensorFreq, SensorRange, " +
                      "     XAcquireEnable, YAcquireEnable, ZAcquireEnable, " +
                      "     XTrigEnable, YTrigEnable, ZTrigEnable, " +
                      "     TrigValue, HighPassCutoffFreq) " +
                      "values(@NodeUuid, @AcquireDataType, " +
                      "     @DataLength, @SensorFreq, @SensorRange, " +
                      "     @XAcquireEnable, @YAcquireEnable, @ZAcquireEnable, " +
                      "     @XTrigEnable, @YTrigEnable, @ZTrigEnable, " +
                      "     @TrigValue, @HighPassCutoffFreq);";
            return SqliteDbHelper.Insert(sql, models.ToList<object>());
        }

        /// <summary>
        /// 更新节点私有属性(不修改节点私有属性Id)
        /// </summary>
        /// <param name="model">节点私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Update(Models.NodePrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set NodeUuid = @NodeUuid, AcquireDataType = @AcquireDataType, " +
                      "     DataLength = @DataLength, SensorFreq = @SensorFreq, SensorRange = @SensorRange, " +
                      "     XAcquireEnable = @XAcquireEnable, YAcquireEnable = @YAcquireEnable, ZAcquireEnable = @ZAcquireEnable, " +
                      "     XTrigEnable = @XTrigEnable, YTrigEnable = @YTrigEnable, ZTrigEnable = @ZTrigEnable, " +
                      "     TrigValue = @TrigValue, HighPassCutoffFreq = @HighPassCutoffFreq " +
                      "where Rid = @Rid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 通过节点编号更新节点私有属性
        /// </summary>
        /// <param name="model">节点私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool UpdateByNodeUuid(Models.NodePrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"update {TableName} " +
                      "set AcquireDataType = @AcquireDataType, " +
                      "     DataLength = @DataLength, SensorFreq = @SensorFreq, SensorRange = @SensorRange, " +
                      "     XAcquireEnable = @XAcquireEnable, YAcquireEnable = @YAcquireEnable, ZAcquireEnable = @ZAcquireEnable, " +
                      "     XTrigEnable = @XTrigEnable, YTrigEnable = @YTrigEnable, ZTrigEnable = @ZTrigEnable, " +
                      "     TrigValue = @TrigValue, HighPassCutoffFreq = @HighPassCutoffFreq " +
                      "where NodeUuid = @NodeUuid;";
            return SqliteDbHelper.Update(sql, model);
        }

        /// <summary>
        /// 删除节点私有属性
        /// </summary>
        /// <param name="model">节点私有属性对象</param>
        /// <returns>true成功，false失败</returns>
        public bool Delete(Models.NodePrivateAttribute model)
        {
            if (model == null)
            {
                return false;
            }

            var sql = $"delete from {TableName} where Rid = @Rid";
            return SqliteDbHelper.Delete(sql, model);
        }

        /// <summary>
        /// 查询节点私有属性
        /// </summary>
        /// <param name="rid">节点私有属性Id</param>
        /// <returns>节点私有属性对象</returns>
        public Models.NodePrivateAttribute GetModel(int rid)
        {
            var sql = $"select * from {TableName} where Rid = @Rid";
            var model = new Models.NodePrivateAttribute() { Rid = rid };
            return SqliteDbHelper.GetDataList<Models.NodePrivateAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询节点私有属性
        /// </summary>
        /// <param name="nodeUuid">节点Id</param>
        /// <returns>节点私有属性对象</returns>
        public Models.NodePrivateAttribute GetModel(string nodeUuid)
        {
            var sql = $"select * from {TableName} where NodeUuid = @NodeUuid";
            var model = new Models.NodePrivateAttribute() { NodeUuid = nodeUuid };
            return SqliteDbHelper.GetDataList<Models.NodePrivateAttribute>(sql, model).FirstOrDefault();
        }

        /// <summary>
        /// 查询所有节点私有属性
        /// </summary>
        /// <returns>节点私有属性对象集合</returns>
        public List<Models.NodePrivateAttribute> GetAllModels()
        {
            var sql = $"select * from {TableName}";
            return SqliteDbHelper.GetDataList<Models.NodePrivateAttribute>(sql);
        }
    }
}
